﻿using Microsoft.Build.Utilities;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using Rhino.Mocks;

using KubertSoft.MSBuildTasks.Subversion.Svn;

namespace KubertSoft.MSBuildTasks.Subversion.Tests
{
    /// <summary>
    ///This is a test class for SvnClientTaskBaseTest and is intended
    ///to contain all SvnClientTaskBaseTest Unit Tests
    ///</summary>
    [TestClass]
    public class SvnClientTaskBaseTest
    {
        /// <summary>
        /// A repository of mocks.
        /// </summary>
        private MockRepository _mocks;

        /// <summary>
        /// A mock that represents build engine.
        /// </summary>
        private BuildEngineStub _buildEngine;

        /// <summary>
        /// Input task items.
        /// </summary>
        private TaskItemDictionary _inputItems;

        /// <summary>
        /// Output task items.
        /// </summary>
        private TaskItemDictionary _outputItems;

        /// <summary>
        /// Gets or sets the test context which provides
        /// information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext { get; set; }

        /// <summary>
        /// Initializes the mocks.
        /// </summary>
        [TestInitialize]
        public void InitializeMocks()
        {
            _mocks = new MockRepository();
            _buildEngine = _mocks.PartialMock<BuildEngineStub>();
            _inputItems = _mocks.StrictMock<TaskItemDictionary>();
            _outputItems = _mocks.StrictMock<TaskItemDictionary>();
        }

        /// <summary>
        /// Cleans mocks.
        /// </summary>
        [TestCleanup]
        public void CleanUpMocks()
        {
            _mocks.VerifyAll();
        }

        [TestMethod]
        public void TestBuildCommandLine()
        {
            var task = _mocks.PartialMock<SvnClientTaskBase>();
            task.CommandLine = new CommandLineBuilder();

            //task.Expect(x => x.CommandLine);
            _mocks.ReplayAll();

            task.BuildCommandLine();

            Assert.AreEqual(CommandLineSwitches.Xml, task.CommandLine.ToString());
        }

        /// <summary>
        /// Tests that there is not input parameters.
        ///</summary>
        [TestMethod]
        public void TestInitializeInputParameters()
        {
            var task = _mocks.PartialMock<SvnClientTaskBase>();
            task.BuildEngine = _buildEngine;
            task.InputTaskItems = _inputItems;

            task.Expect(x => x.InitializeInputParameters());
            _mocks.ReplayAll();

            task.InitializeInputParameters();

            Assert.AreEqual(0, _inputItems.Count);
            Assert.AreEqual(0, _buildEngine.ErrorsCount);
        }

        /// <summary>
        /// Test for Xml output parameter.
        ///</summary>
        [TestMethod]
        public void TestInitializeOutputParameters()
        {
            var task = _mocks.PartialMock<SvnClientTaskBase>();
            task.BuildEngine = _buildEngine;
            task.OutputTaskItems = _outputItems;
            task.XmlDocumentHelper = CreateXmlDocumentHelperStub();

            _mocks.ReplayAll();

            task.InitializeOutputParameters();

            Assert.AreEqual(1, _outputItems.Count);
            Assert.AreEqual(0, _buildEngine.ErrorsCount);
        }

        /// <summary>
        /// Test for Xml output parameter with no xml output.
        ///</summary>
        [TestMethod]
        public void TestInitializeOutputParametersWithNoXml()
        {
            var task = _mocks.PartialMock<SvnClientTaskBase>();
            task.BuildEngine = _buildEngine;
            task.OutputTaskItems = _outputItems;
            task.XmlDocumentHelper = new XmlDocumentHelper();

            _mocks.ReplayAll();

            task.InitializeOutputParameters();

            Assert.AreEqual(1, _outputItems.Count);
            Assert.AreEqual(1, _buildEngine.ErrorsCount);
        }

        /// <summary>
        /// Creates the XML document helper stub.
        /// </summary>
        /// <returns></returns>
        private static XmlDocumentHelper CreateXmlDocumentHelperStub()
        {
            var result = new XmlDocumentHelper();
            result.AppendLine(@"<?xml version=""1.0"" encoding=""utf-8""?>");
            result.AppendLine("<root />");
            return result;
        }
    }
}